package com.example.quanxin.entity;/*
 *
 *   welcome:11311
 *   创建时间:2023/5/20
 *
 */

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.ObjectUtils;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ReturnT<T> implements Serializable {

    @Schema(defaultValue = "状态")
    private Integer code;

    @Schema(defaultValue = "结果")
    private String msg;

    @Schema(defaultValue = "数据")
    private T data;

    public static ReturnT SUCCESS(){
        return ReturnT.builder()
                .code(200)
                .data(null)
                .msg("")
                .build();
    }

    public static ReturnT SUCCESS(Object data){
        return ReturnT.builder()
                .code(200)
                .data(data)
                .msg("")
                .build();
    }

    public static ReturnT SUCCESS(Object data, String msg){
        return ReturnT.builder()
                .code(200)
                .data(data)
                .msg(msg)
                .build();
    }

    public static ReturnT FAIL(){
        return ReturnT.builder()
                .code(500)
                .data(null)
                .msg("")
                .build();
    }

    public static <T> ReturnT<T> ok(T data) {
        return restResult(data, 200, "success");
    }

    private static <T> ReturnT<T> restResult(T data, int code, String msg) {
        ReturnT<T> apiResult = new ReturnT<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        if (ObjectUtils.isEmpty(msg)) {
            apiResult.setMsg("失败");
        } else {
            apiResult.setMsg(msg);
        }

        return apiResult;
    }


}
